<?php
namespace App\Http\Controllers;

use App\Service\QdService;
use App\Service\ScGoodsService;
use App\Service\UserService;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class ExcelController extends Controller
{
    //测试导出
    public function test()
    {
        $cellData = [
            ['学号','姓名','成绩'],
            ['10001','AAAAA','99'],
            ['10002','BBBBB','92'],
            ['10003','CCCCC','95'],
            ['10004','DDDDD','89'],
            ['10005','EEEEE','96'],
        ];
        //return Excel::download($cellData, '人员导出.xlsx');
        $fname = 'test';
        $fext = 'xlsx';
        Excel::create($fname,function($excel) use ($cellData){
            $excel->sheet('score', function($sheet) use ($cellData){
                $sheet->rows($cellData);
            });
        })->export($fext);
        $fpath = $fname . '.' . $fext;
        return $this->toJson(0, '', $fpath);
    }

    //导出用户
    public function user(Request $request)
    {
        header("Access-Control-Allow-Origin: *");
        $pas['phone'] = $request->get('phone', '');
        $pas['nick'] = $request->get('nick', '');
        $res = (new UserService())->getUlist($pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        $res = $res['data'];
        $fname = '用户'.time();
        $arys[] = [
            'ID', '昵称', '手机号', '头像地址', '钱包余额',
            '当前等级', '浏览量', '收货地址', '优惠劵数量', '充值量', '时间'
        ];
        foreach ($res as $v) {
            $arys[] = [
                $v['id'].'', $v['nickname'].'', $v['phone'] ,$v['balance'], $v['levelName'],
                $v['readNum'], $v['addressNum'], $v['couponNum'] ,$v['rechargeNum'], $v['lasttime'],
            ];;
        }
        Excel::create($fname,function($excel) use ($arys){
            $excel->sheet('sheet', function($sheet) use ($arys){
                $sheet->rows($arys);
            });
        })->export('xlsx');
    }

    //导出零售分类
    public function goodsCate(Request $request)
    {
        header("Access-Control-Allow-Origin: *");
        $res = (new ScGoodsService())->getClist();
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        $res = $res['data'];
        $fname = '零售分类'.time();
        $arys[] = [
            '分类名称', '图标', '父级名', '最近时间',
        ];
        foreach ($res as $v) {
            $arys[] = [
                $v['title'], $v['img'], $v['pname'], $v['uptime'],
            ];
        }
        Excel::create($fname,function($excel) use ($arys){
            $excel->sheet('sheet', function($sheet) use ($arys){
                $sheet->rows($arys);
            });
        })->export('xlsx');
    }

    //导出零售商品
    public function goods(Request $request)
    {
        header("Access-Control-Allow-Origin: *");
        $res =(new ScGoodsService())->getGlist();
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        $res = $res['data'];
        $fname = '零售商品'.time();
        $arys[] = [
            '商品图片', '商品编号', '商品名称', '商品分类', '商品销量', '是否为上新',
        ];
        foreach ($res as $v) {
            $arys[] = [
                $v['img'], $v['gno'], $v['title'], $v['cate_name'], $v['sale'], $v['isnew'],
            ];
        }
        Excel::create($fname,function($excel) use ($arys){
            $excel->sheet('sheet', function($sheet) use ($arys){
                $sheet->rows($arys);
            });
        })->export('xlsx');
    }

    //导出零售订单
    public function goodsOrder(Request $request)
    {
        header("Access-Control-Allow-Origin: *");
        $pas['keyword'] = $request->get('keyword', '');
        $pas['phone'] = $request->get('phone', '');
        $pas['status'] = $request->get('status', 0);
        $res = (new ScGoodsService())->getOlist($pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        $res = $res['data'];
        $fname = '零售订单'.time();
        $arys[] = [
            '订单号', '下单用户', '手机号', '商品名称', '商品图片',
            'SKU名称', '单价(元)', '数量', '实付款(元)', '收件人', '收件人地址',
        ];
        foreach ($res as $v) {
            $arys[] = [
                $v['orderno'], $v['uname'], $v['phone'], $v['gname'],
                $v['gimg'], $v['sku_name'], $v['price'], $v['num'],
                $v['real_pay'], $v['accepter'], $v['acceptDoor'],
            ];
        }
        Excel::create($fname,function($excel) use ($arys){
            $excel->sheet('sheet', function($sheet) use ($arys){
                $sheet->rows($arys);
            });
        })->export('xlsx');
    }

    //导出渠道分类
    public function qdCate(Request $request)
    {
        header("Access-Control-Allow-Origin: *");
        $res = (new QdService())->getClist();
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        $res = $res['data'];
        $fname = '渠道分类'.time();
        $arys[] = [
            '父级', '分类名称', '图标', '最近时间'
        ];
        foreach ($res as $v) {
            $arys[] = [
                $v['pname'], $v['title'], $v['img'], $v['uptime'],
            ];
        }
        Excel::create($fname,function($excel) use ($arys){
            $excel->sheet('sheet', function($sheet) use ($arys){
                $sheet->rows($arys);
            });
        })->export('xlsx');
    }

    //导出渠道商品
    public function qdGoods(Request $request)
    {
        header("Access-Control-Allow-Origin: *");
        $res =(new QdService())->getGlist();
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        $res = $res['data'];
        $fname = '渠道商品'.time();
        $arys[] = [
            'ID', '商品图片',
            //iconv('GBK', 'UTF-8', '商品图片'),
            '商品编号', '商品名称', '商品分类', '商品销量', '是否为上新'
        ];
        foreach ($res as $v) {
            $arys[] = [
                $v['id'], $v['img'], $v['gno'], $v['title'], $v['cate_name'],
                $v['sale'], $v['isnew'],
            ];
        }
        Excel::create($fname,function($excel) use ($arys){
            $excel->sheet('sheet', function($sheet) use ($arys){
                $sheet->rows($arys);
            });
        })->export('xlsx');
    }

    //导出渠道订单
    public function qdOrder(Request $request)
    {
        header("Access-Control-Allow-Origin: *");
        $res = (new QdService())->getOlist();
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        $res = $res['data'];
        $fname = '渠道订单'.time();
        $arys[] = [
            '订单号', '渠道商', '商品信息', '总价(元)', '收件人', '收件人地址', '交易状态'
        ];
        foreach ($res as $v) {
            $arys[] = [
                $v['orderno'], $v['uname'], $v['ginfo'], $v['price'],
                $v['accepter'], $v['accept_address'], $v['status_name'],
            ];
        }
        Excel::create($fname,function($excel) use ($arys){
            $excel->sheet('sheet', function($sheet) use ($arys){
                $sheet->rows($arys);
            });
        })->export('xlsx');
    }
}